<?php
namespace app\admin\controller;

class Login  extends \think\Controller
{
    //登录页面
    public function index()
    {
        return view();
    }

    public function admincheck()
    {
         //获取全部数据
         $account=input('post.account','','strip_tags');
         $password=input('post.password','','htmlspecialchars,md5');
         $data = ['account'=>$account,'password'=>$password];
         $validate = validate('Admin');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
         $ret = db('admin')->where("account='$account' and password='$password'")->find();
         if($ret==null){
            $this->error('用户名密码错误');
         }else{
                         //获取角色
            $adminRole = model('AdminRole');
            $role_ids = $adminRole->getRoleIds($ret['id']);
            // dump($role_ids);die;
            //获取权限
            $rolePermission =model('RolePermission');
            $permission_ids = $rolePermission->getPermissionIds($role_ids);
            //dump($permission_ids);die;

            session('role_ids',$role_ids);
            session('permission_ids',$permission_ids);
            session('admin_id',$ret['id']);
            session('account',$ret['account']);
            session('login_time',time());
            session('nickname',$ret['nickname']);
            session('head_img',$ret['head_img']);
            $this->success('登录成功','admin/index/index');  
         }
       
    }

    public function loginout()
    {
        session('role_ids',null);
        session('permission_ids',null);
        session('admin_id',null);
        session('account',null);
        session('login_time',null);
        session('nickname',null);
        session('head_img',null);
        $this->success('退出成功','admin/login/index');  
    }
}
